#!/bin/bash
PATH="$PATH:/usr/lib/dkms"
ASCEND_SECLOG="/var/log/ascend_seclog"
logFile="${ASCEND_SECLOG}/ascend_install.log"
installInfo="/etc/ascend_install.info"

SHELL_DIR=$(cd "$(dirname "$0")" || exit; pwd)
COMMON_SHELL="$SHELL_DIR/common.sh"
# load common.sh, get install.info
source "${COMMON_SHELL}"
# read Driver_Install_Path_Param from installInfo
Driver_Install_Path_Param=$(getInstallParam "Driver_Install_Path_Param" "${installInfo}")

targetdir="${Driver_Install_Path_Param}"
targetdir="${targetdir%*/}"

module_name=davinci_ascend
module_version=1.0
log() {
    cur_date=`date +"%Y-%m-%d %H:%M:%S"`
    user_id=`id | awk '{printf $1}'`
    echo "[Driver] [$cur_date] [$user_id] "$1 >> $logFile
    return 0
}

drvColorEcho() {
   cur_date=`date +"%Y-%m-%d %H:%M:%S"`
   echo -e  "[Driver] [$cur_date] $1"
}

# get dkms output path
ko_output_absolute_path(){
    local pcie_ko_path="$(find /lib/modules/$(uname -r) -type f -name "drv_pcie_host.ko*")"
    if [ x"${pcie_ko_path}" == x"" ]; then
        pcie_ko_path="$(find /lib/modules/$(uname -r) -type f -name "drv_davinci_intf_host.ko*")"
    fi
    local absolute_path="${pcie_ko_path%/*}"
    echo $absolute_path
    return 0
}

# $1 : module_name
dkms_remove() {
    dkms status | grep $1 >/dev/null
    if [ $? -eq 0 ];then
        tmp_version=$(dkms status | grep $1 | grep $(uname -r) | awk -F [/' '] '{print $2}' | awk -F [,:] '{print $1}')
        if [ "$tmp_version"x = ""x ];then
            tmp_version=$(dkms status | grep $1 | grep "added" | awk -F [/' '] '{print $2}' | awk -F [,:] '{print $1}')
            if [ "$tmp_version"x = ""x ];then
                tmp_version=$(dkms status | grep $1 | head -n 1 | awk -F [/' '] '{print $2}' | awk -F [,:] '{print $1}')
            fi
        fi

        if [ "$tmp_version"x = ""x ];then
            log "[WARNING]dkms version get faild"
            log "[WARNING]dkms status : "
            dkms status | grep $1 >>$logFile 2>&1
            return
        fi

        log "[INFO]davinci hiai kernel driver is installed($1-$tmp_version), delete and reinstall"

        dkms uninstall -m $1 -v $tmp_version --no-initrd >>$logFile 2>&1
        dkms remove -m $1 -v $tmp_version --all --no-initrd >>$logFile 2>&1
        log "[INFO]dkms remove success"

        tmp_src_dir=/usr/src/$1-$tmp_version
        if [ -d $tmp_src_dir ];then
            rm -rf $tmp_src_dir
            log "[INFO]rm -rf $tmp_src_dir success"
        fi
    fi

    return 0
}

dkms_copy_source() {
    sources="$targetdir"/driver/kernel
    if [ ! -d "$sources" ];then
        log "[ERROR]hiai kernel driver source missing, failed"
        return 1
    fi

    module_name=$(cat "$sources"/dkms.conf | grep PACKAGE_NAME | awk -F '"' '{print $2}')
    module_version=$(cat "$sources"/dkms.conf | grep PACKAGE_VERSION | awk -F '"' '{print $2}')
    log "[INFO]dkms module name : $module_name, version : $module_version"

    dkms_remove $module_name

    src_dir=/usr/src/$module_name-$module_version
    if [ -d $src_dir ];then
        rm -rf $src_dir
        log "[INFO]rm -rf $src_dir success"
    fi

    mkdir -p $src_dir
    if [ $? -ne 0 ];then
        log "[ERROR]mkdir -p $src_dir failed"
        return 1
    fi
    log "[INFO]create $src_dir success"

    cp -rf "$sources"/* $src_dir/
    if [ $? -ne 0 ];then
        log "[ERROR]cp -rf $sources/ $src_dir/ failed"
        return 1
    fi
    log "[INFO]cp -rf $sources/ $src_dir/ success"

    return 0
}

dkms_add() {
    log "[INFO]run dkms add command..."
    dkms add -m $module_name -v $module_version >>$logFile 2>&1
    return 0
}

dkms_build() {
    log "[INFO]run dkms build command..."
    dkms build -m $module_name -v $module_version >>$logFile 2>&1
    return 0
}

dkms_install() {
    log "[INFO]run dkms install command..."
    dkms install -m $module_name -v $module_version --no-initrd >>$logFile 2>&1
    return 0
}

dkms_output_check() {
    sys_dir=$(ko_output_absolute_path)
    if [ ! -d $sys_dir ]; then
        if [ ! -d $sys_dir ]; then
            log "[ERROR]dkms output is NONE, dkms failed"
            return 1
        fi
    fi

    local module_full_name="" ko=""
    for ko in ${g_kos}
    do
        if [ ! -f "/etc/uvp-release" ] && [ "${ko##*/}" == "drv_vascend.ko" ];then
            continue
        fi
        module_full_name=${ko##*/}
        ls ${sys_dir} | grep -q ${module_full_name}
        if [ $? -ne 0 ]; then
            log "[ERROR]There has no ${sys_dir}/${module_full_name}, dkms install failed"
            return 1
        else
            chmod -f 440 ${sys_dir}/${module_full_name}*
        fi
    done

    log "[INFO]dkms output files check success"
    return 0
}

add_readme_to_install_path() {
    if [ -d "$targetdir"/driver/host ];then
        echo "warning:" >"$targetdir"/driver/host/README.txt
        echo "davinci hiai host kernel driver is managed by dkms, driver ko in this path is no more effective." >>"$targetdir"/driver/host/README.txt
        echo "driver ko installed by dkms is in : $sys_dir." >>"$targetdir"/driver/host/README.txt
    fi

    log "[INFO]run install path add $targetdir/driver/host/README.txt success"
    return 0
}

dkms_copy_source
if [ $? -ne 0 ];then
    exit 1
fi

log "[INFO]driver dkms log >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"

dkms_add
dkms_build
dkms_install

log "[INFO]driver dkms log <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"

dkms_output_check
if [ $? -ne 0 ];then
    drvColorEcho "[ERROR]\033[31mDkms install failed, details in : $logFile \033[0m"
    log "[ERROR]dkms install files check failed"
    exit 1
fi

add_readme_to_install_path

exit 0

